package opn.srn.common;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class SimpleMessage implements IMessage {

	Map<String, Object> datas = new LinkedHashMap<String, Object>();

	@Override
	public <T> void setData(String name, T value) {
		datas.put(name, value);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getData(String name) {
		return (T) datas.get(name);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T remove(String name) {
		return (T) datas.remove(name);
	}

	@Override
	public void clear() {
		datas.clear();
	}

	@Override
	public Map<String, Object> getAllDatas() {
		return datas;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		for (Entry<String, Object> data : datas.entrySet()) {
			buffer.append(data.getKey() + " : " + data.getValue() + ", ");
		}
		return buffer.toString();
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		SimpleMessage newInstance = new SimpleMessage();
		newInstance.datas.putAll(datas); // ..Assuming all the values are immutable
		return newInstance;
	}

}
